package com.leostadyn.stackfarm.timestatistics.view.activity.employee;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.SelectListAdapter;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.model.bean.PostBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.ProjectBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ProjectSelectActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView confirm;
    private RecyclerView rv_select_project;
    private SelectListAdapter adapter;
    private ArrayList<String> mProjectIds = new ArrayList<>();
    private ArrayList<String> mPostIds = new ArrayList<>();
    private ArrayList<String> mPostNames = new ArrayList<>();
    private List<ProjectBean> listdata = new ArrayList<>();
    private ArrayList<PostBean> postdata = new ArrayList<>();
    private String mIndex = "-1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_list2);
        mIndex = getIntent().getStringExtra("mIndex");
        initProjectData();
        initView();
    }

    public void initView() {
        title = findViewById(R.id.nav_txt_title);
        title.setText("选择项目");

        confirm = findViewById(R.id.nav_btn_add);
        confirm.setVisibility(View.VISIBLE);
        confirm.setText("确定");
        confirm.setOnClickListener(this);

        findViewById(R.id.nav_img_back).setOnClickListener(v -> {
            finish();
        });

        rv_select_project = findViewById(R.id.rv_select_project);

        rv_select_project.setLayoutManager(new LinearLayoutManager(this));

    }

    public void initProjectData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("status", "1");
        AdminApi.getInstance().getProjectList(map).subscribe(new OriginalSubscriber<List<ProjectBean>>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                ToastUtil.show(ProjectSelectActivity.this, "项目获取失败");
            }

            @Override
            public void onNext(List<ProjectBean> projectBeans, String msg) {
                listdata = projectBeans;
                if (listdata != null) {
                    for (int i = 0; i < listdata.size(); i++) {
                        ProjectBean project = projectBeans.get(i);
                        postdata.add(new PostBean(project.getId(), project.getName(), project.getCode(), false));
                    }
                    mPostIds.add("-1");
                    adapter = new SelectListAdapter(ProjectSelectActivity.this, postdata, mPostIds);
                    adapter.setmIndex(mIndex);

                    rv_select_project.setAdapter(adapter);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_btn_add:
                if (adapter.getPostidLists() != null && adapter.getPostidLists().size() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra("id", adapter.getProjectIdList().get(0));
                    intent.putExtra("name", adapter.getNameLists().get(0));
                    intent.putExtra("code", adapter.getCodeLists().get(0));
                    /*intent.putExtra("index", mIndex);*/
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    ToastUtil.show(this, "请选择项目");
                }
        }
    }
}
